package com.ayoka.mvp;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.ayoka.base.BasePresenter;
import com.ayoka.base.IBaseView;
import com.ayoka.bean.BaseResponse;
import com.ayoka.bean.UserInfoBean;
import com.ayoka.http.API;
import com.ayoka.http.OkHttpListener;
import com.ayoka.mvp.CourseQuestionMVP.CourseQuestionModel.CourseQuestionBean;
import com.ayoka.ui.course.detail.QuestionInputDialog;
import com.jcp.baselibrary.dialog.BaseDialogFragment;
import com.jcp.baselibrary.dialog.DialogResultListener;

import java.util.List;

/**
 * Created by gabriel on 2017/3/5.
 */

public interface CourseQuestionMVP {

    interface CourseQuestionModel {


        class CourseQuestionBean{
            public String Id;
            public long CourseID;
            public long Uid;
            public UserInfoBean UserInfo;
            public String Content;
            public int IsReply;
            public String Reply;
            public String CreateDate;
        }
    }

    interface ICourseQuestionView extends IBaseView {

        void configCourseQuestionListData(List<CourseQuestionBean> courseQuestionBeanList);

        void submitQuestionCallback(boolean isSuccess);
    }

    class CourseQuestionPresenter extends BasePresenter<ICourseQuestionView> {


        public void getCourseQuestionListData(long courseId) {
            API.getCourseQuestionListData(this, courseId, new OkHttpListener<BaseResponse<List<CourseQuestionBean>>>() {
                @Override
                public void onSuccess(BaseResponse<List<CourseQuestionBean>> response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().configCourseQuestionListData(response.data);
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                }
            });
        }

        public void showQuestionInputDialog(FragmentManager fragmentManager, final long courseId) {
            //弹出评论输入框
            QuestionInputDialog dialog = new QuestionInputDialog.Builder()
                    .setAnimation(0)
                    .setGravity(Gravity.BOTTOM)
                    .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setCourseId(courseId)
                    .build();
            dialog.setDialogResultListener(new DialogResultListener() {
                @Override
                public void result(BaseDialogFragment DialogFragment, Object result) {
                }
            });
            dialog.show(fragmentManager, "QuestionInputDialog");
        }

        public void submitQuestion(long courseId, String question) {
            API.submitCourseQuestionData(this, courseId, question, new OkHttpListener<BaseResponse>() {
                @Override
                public void onSuccess(BaseResponse response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().submitQuestionCallback(true);
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                }
            });
        }
    }
}
